/*
 * SplashWidget.fx
 *
 * Created on 09-Feb-2009, 12:04:57
 */

package widgets;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.Interpolator;
import java.lang.StringBuilder;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Line;
import javafx.scene.paint.Color;
import widgets.NextStepButtonWidget;

/**
 * @author beol
 */

public class SplashWidget extends CustomNode {

    public var x: Number = 0;
    public var y: Number = 0;

    def tempString = bind addChar(counter);

    var transperentOfLine: Number = 0.0;

    var transperentOfTextZero: Number = 0.0;
    var transperentOfTextOne: Number = 0.0;
    var transperentOfTextTwo: Number = 0.0;
    var transperentOfTextThree: Number = 0.0;
    var transperentOfGoButton:Number = 0.0;

    var counter:Number = 0;

    def welcomeMSG:String = "добро пожаловать";

    bound function addChar(counter:Number):String {
        var cs = welcomeMSG.subSequence(0, counter);
        return cs.toString();
    }

    var welcomeText: Text = Text {
        translateX: x + 10
        translateY: y
        font: Font {
            size: 18
        }
        content: bind tempString;
    }
    var introdutionTextZero: Text = Text {
        font: Font {
            size: 14
        }
        opacity: bind transperentOfTextZero;
        x: x,
        y: y + 30
        content: "этапы составления резюме:";
    }

    var introdutionTextOne: Text = Text {
        font: Font {
            size: 14
        }
        opacity: bind transperentOfTextOne;
        x: x,
        y: y + 48
        content: "1) импорт данных";
    }
    var introdutionTextTwo: Text = Text {
        font: Font {
            size: 14
        }
        opacity: bind transperentOfTextTwo;
        x: x,
        y: y + 66
        content: "2) редактирование";
    }
    var introdutionTextThree: Text = Text {
        font: Font {
            size: 14
        }
        opacity: bind transperentOfTextThree;
        x: x,
        y: y + 84
        content: "3) экспорт результата";
    }

    var line: Line = Line {
        startX: x + 10 - 2,
        startY: y + 2
        endX: x + 177,
        endY: y + 2
        strokeWidth: 1
        stroke: Color.BLACK
        opacity: bind transperentOfLine
    }

    var introAnimation: Timeline = Timeline {
        repeatCount: 1
        keyFrames: [
            KeyFrame{
                time: 1s;
                values: counter => 0;
            },
            KeyFrame{
                time: 3s;
                values: [counter => 16 tween Interpolator.EASEIN, transperentOfLine => 0.0];
            },
            KeyFrame {
                time: 4s;
                values: [transperentOfLine => 1.0 tween Interpolator.LINEAR, transperentOfTextZero => 0.0];
            }

            KeyFrame {
                time: 5s;
                values: [transperentOfTextZero => 1.0 tween Interpolator.LINEAR, transperentOfTextOne => 0];
            }

            KeyFrame {
                time: 6s;
                values: [transperentOfTextOne => 1.0 tween Interpolator.LINEAR, transperentOfTextTwo => 0];
            }

            KeyFrame {
                time: 7s;
                values: [transperentOfTextTwo => 1.0 tween Interpolator.LINEAR, transperentOfTextThree => 0];
            }

            KeyFrame {
                time: 8s;
                values: [transperentOfTextThree => 1.0 tween Interpolator.LINEAR, transperentOfGoButton => 0];
            }

            KeyFrame {
                time: 9s;
                values: transperentOfGoButton => 1.0 tween Interpolator.LINEAR;
            }

        ]
    }

    var Yoffset: Number = y on replace do {
            //welcomeText.opacity = welcomeText.opacity - 0.1;
            //line.opacity
            content.translateY -= Yoffset;
    };

    var outroAnimation: Timeline = Timeline {
        keyFrames: [
            KeyFrame{
                time: 0s;
                values: [Yoffset => 0];
            },
            KeyFrame{
                time: 1s;
                values: [Yoffset => 50 tween Interpolator.LINEAR];
                action: function() {
                    content.content = [];
                    isSplashFinished = true;
                }
            },
        ]
    }

    def content: Group = Group {
    }

    def isPressed: Boolean = bind goButton.isPressed on replace do {
        //outroAnimation.play();
        if (isPressed == true) {
        println("pressed");
        outroAnimation.play();
        }
    }

    def goButton:NextStepButtonWidget = NextStepButtonWidget {
        phrase: "поехали!"
        x: x + 24
        y: y + 110
        textXoffset: 33
        textYoffset: 21
        opacity: bind transperentOfGoButton
    }

    public var isSplashFinished: Boolean = false;

    protected override function create(): Node {

        content.content = [welcomeText,
                           line,
                           introdutionTextZero,
                           introdutionTextOne,
                           introdutionTextTwo,
                           introdutionTextThree,
                           goButton];
        return content;
    }

    public function play():Void {
        introAnimation.play();
    }
}
